<template>
  <view class="page">
    <!-- 公共组件-每个页面必须引入 -->
    <public-module></public-module>

    <view class="u-padding-20" v-if="!userInfo.token">
      <u-empty :text="$t('nologin')" mode="permission"></u-empty>
    </view>

    <view class="u-padding-20" v-else>
	<!--u-loading-page :loading="loadstatus"></u-loading-page-->

	<u--form labelPosition="left" ref="uForm" labelWidth="100px">
		
		<u-form-item :label="$t('驾驶证类别')" prop="confirmpassword">
			<view @click="drivingcerttypeshow=true" style="border:solid 1px #ccc;width:100%;padding:6px;">
				<text v-if="userInfo.drivingcerttype==''||userInfo.drivingcerttype==null">请选择</text> {{userInfo.drivingcerttype}}
			</view>
			<u-picker
			:confirmText="$t('confirm')"
			:cancelText="$t('cancel')"
			:show="drivingcerttypeshow" ref="uPicker" :columns="drivingcerttype" @cancel="drivingcerttypeshow=false" @confirm="drivingcerttypeconfirm"></u-picker>
		</u-form-item>
		<u-form-item :label="$t('免许证番号')">
			<u--input v-model="userInfo.drivingcertcode" type="text" />
		</u-form-item>
		<u-form-item :label="$t('免许证取得日')">
			<u--input v-model="userInfo.drivingcertstart" type="text" />
		</u-form-item>
		<u-form-item :label="$t('免许证有效期')">
			<u--input v-model="userInfo.drivingcertend" type="text" />
		</u-form-item>
			<u-form-item :label="$t('cert_img1')" borderBottom>
					<u-upload 
						:fileList="fileList1"
						@afterRead="afterRead"
						maxCount="1"
						@delete="deletePic"
						name="1"
						width="180"
						height="120">
					</u-upload>
			</u-form-item>
			<u-form-item :label="$t('cert_img2')" :borderBottom="false">
					<u-upload 
						:fileList="fileList2"
						@afterRead="afterRead"
						maxCount="1"
						@delete="deletePic"
						name="2"
						width="180"
						height="120">
					</u-upload>
			</u-form-item>
	</u--form>

	<u-gap height="20"></u-gap>
	<u-button type="primary" @click="userlist_edit_submit" :disabled="loadstatus" :loading="loadstatus" loadingText="...">{{$t('modify')}}</u-button>
	<u-gap height="10"></u-gap>



    </view>
  </view>
</template>


<script>
import { mapState, mapMutations } from "vuex"
import { api_editUserinfo,api_user_region } from '@/config/api.js'
import addressd from "@/components/jm-address/jm-address.vue"

import { uploadFilePromise } from '@/plugins/upload/upload';
export default {
    components: {
        addressd
    },
  data() {
    return {
      hasLogin: false,
      loadStatus: "loadmore",
      loadstatus: false,
      loadstxt: "loading...",
	  model:{},
	  fileList1:[],
	  fileList2:[],
			drivingcerttypeshow:false,
			drivingcerttype:[
                    [this.$t('驾驶证'), this.$t('身份证')]
                ],
			birthday:''
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  onLoad(options) {
	  console.log(this.userInfo)
	  
				if (this.userInfo.idcard_1!=''){
					this.fileList1.push({
						file_id:0,
						url:this.userInfo.idcard_1,
					})
				}
				if (this.userInfo.idcard_2!=''){
					this.fileList2.push({
						file_id:1,
						url:this.userInfo.idcard_2,
					})
				}
				
				// res.images.map((item) => {
				// 	console.log(item)
				// 	this.fileList2.push({
				// 		file_id:item.file_id,
				// 		url:item.file_url
				// 	})
				// })
	  
	  
  },

  methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await uploadFilePromise(lists[i].url,2)
					let item = this[`fileList${event.name}`][fileListLen]
					
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						file_id: result.file_id,
						url: result.file_url
					}))
					//this[`fileList${event.name}`][fileListLen].url=result
					fileListLen++
				}
			},
	  drivingcerttypeconfirm(e){
		  this.drivingcerttypeshow=false
		  this.userInfo.drivingcerttype=e.value[0]
		  this.show = false
	  },

    userlist_edit_submit() {
		
		// var cover_id = ''
		// if (this.fileList1.length>0){
		// 	this.fileList1.map((item) => {
		// 		cover_id=item.file_id
		// 	})
		// }
		// let images = []
		// this.fileList2.map((item) => {
		// 	images.push({
		// 		file_url:item.url,
		// 		file_id:item.file_id
		// 	})
		// })
		console.log(this.fileList1)
		console.log(this.fileList2)
		var idcard_1 = ''
		var idcard_2 = ''
		if (this.fileList1.length>0){
			this.fileList1.map((item) => {
				idcard_1=item.url
			})
		}
		let images = []
		this.fileList2.map((item) => {
			idcard_2=item.url
		})
		
		api_editUserinfo({
			userinfoedit:JSON.stringify({}),
			member_wechatedit:JSON.stringify({}),
			drivingcerttype:this.userInfo.drivingcerttype,
			drivingcertcode:this.userInfo.drivingcertcode,
			drivingcertstart:this.userInfo.drivingcertstart,
			drivingcertend:this.userInfo.drivingcertend,
			idcard_1:idcard_1,
			idcard_2:idcard_2
		}).then((res) => {
			this.$tools.msg('修改成功')
			this.$store.dispatch("getUserInfo",{member_id:this.userInfo.member_id});
		}).catch((err) =>{
			
		})
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/.u-form-item__body{
	padding: 3px 0px;
}
</style>
